//package com.sdhs.mob.utils;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import org.apache.commons.collections.MapUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//
//import java.io.*;
//import java.net.HttpURLConnection;
//import java.net.URL;
//import java.net.URLConnection;
//import java.util.Map;
//
//
///**
// * http请求类
// *
// * @author seer
// * @date 2017/12/10 14:16
// */
//public class HttpUtil {
//
//    private static Logger LOGGER = LogManager.getLogger(HttpUtil.class);
//
//    private static String CHARSET = "UTF-8";
//
//    /**
//     * json post请求
//     *
//     * @param url
//     * @param postStr
//     * @return
//     * @throws Exception
//     */
//    public static String doJsonPost(String url, String postStr, Map<String, String> header)
//            throws Exception {
//        return doPost(url, postStr, header, CHARSET, "application/json");
//    }
//
//    /**
//     * 普通post请求
//     *
//     * @param url
//     * @param postStr
//     * @return
//     * @throws Exception
//     */
//    public static String doPost(String url, String postStr)
//            throws Exception {
//        return doPost(url, postStr, null, CHARSET, null);
//    }
//
//    /**
//     * do post request
//     *
//     * @param url
//     * @param postStr
//     * @param header
//     * @param charset
//     * @return
//     * @throws Exception
//     */
//    public static String doPost(String url, String postStr, Map<String, String> header, String charset, String contentType)
//            throws Exception {
//        LOGGER.debug("http post url: " + url);
//        StringBuffer parameterBuffer = new StringBuffer();
//        if (postStr != null) {
//            // 避免日志中出现大量请求日志，例如Ocr识别请求中
//            if (postStr.length() <= 1024) {
//                LOGGER.info("http post request: " + postStr);
//            }
//            parameterBuffer.append(postStr);
//        }
//        URL localURL = new URL(url.trim());
//        URLConnection connection = localURL.openConnection();
//        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
//        httpURLConnection.setDoOutput(true);
//        httpURLConnection.setRequestMethod("POST");
//        httpURLConnection.setRequestProperty("Accept-Charset", charset);
//        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(parameterBuffer.length()));
//        if (contentType == null) {
//            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//        } else {
//            httpURLConnection.setRequestProperty("Content-Type", contentType);
//        }
//        if (MapUtils.isNotEmpty(header)) {
//            for (Map.Entry<String, String> entry : header.entrySet()) {
//                httpURLConnection.setRequestProperty(entry.getKey(), entry.getValue());
//            }
//        }
//        OutputStream outputStream = null;
//        OutputStreamWriter outputStreamWriter = null;
//        InputStream inputStream = null;
//        InputStreamReader inputStreamReader = null;
//        BufferedReader reader = null;
//        StringBuffer resultBuffer = new StringBuffer();
//        String tempLine;
//        try {
//            if (postStr != null) {
//                outputStream = httpURLConnection.getOutputStream();
//                outputStreamWriter = new OutputStreamWriter(outputStream, charset);
//
//                outputStreamWriter.write(parameterBuffer.toString());
//                outputStreamWriter.flush();
//            }
//            LOGGER.debug("http post response code: " + httpURLConnection.getResponseCode());
//            if (httpURLConnection.getResponseCode() >= 300) {
//                throw new Exception("http post failed, response code " + httpURLConnection.getResponseCode());
//            }
//
//            inputStream = httpURLConnection.getInputStream();
//            inputStreamReader = new InputStreamReader(inputStream, charset);
//            reader = new BufferedReader(inputStreamReader);
//
//            while ((tempLine = reader.readLine()) != null) {
//                resultBuffer.append(tempLine);
//            }
//        } finally {
//            if (outputStreamWriter != null) {
//                outputStreamWriter.close();
//            }
//            if (outputStream != null) {
//                outputStream.close();
//            }
//
//            if (reader != null) {
//                reader.close();
//            }
//
//            if (inputStreamReader != null) {
//                inputStreamReader.close();
//            }
//
//            if (inputStream != null) {
//                inputStream.close();
//            }
//        }
//        LOGGER.info("http post response: " + resultBuffer.toString());
//        return resultBuffer.toString();
//    }
//
//    /**
//     * do get request
//     *
//     * @author seer
//     * @date 2017/12/10 14:43
//     */
//    public static String doGet(String url) throws Exception {
//        LOGGER.debug("http get url: " + url);
//        URL localURL = new URL(url.trim());
//        URLConnection connection = localURL.openConnection();
//        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
//
//        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
//        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//        InputStream inputStream = null;
//        InputStreamReader inputStreamReader = null;
//        BufferedReader reader = null;
//        StringBuffer resultBuffer = new StringBuffer();
//        String tempLine;
//        LOGGER.debug("http get response code: " + httpURLConnection.getResponseCode());
//        if (httpURLConnection.getResponseCode() >= 300) {
//            throw new Exception("http get failed, response code " + httpURLConnection.getResponseCode());
//        }
//        try {
//            inputStream = httpURLConnection.getInputStream();
//            inputStreamReader = new InputStreamReader(inputStream, CHARSET);
//            reader = new BufferedReader(inputStreamReader);
//            while ((tempLine = reader.readLine()) != null) {
//                resultBuffer.append(tempLine);
//            }
//        } finally {
//            if (reader != null) {
//                reader.close();
//            }
//            if (inputStreamReader != null) {
//                inputStreamReader.close();
//            }
//            if (inputStream != null) {
//                inputStream.close();
//            }
//        }
//        LOGGER.info("http get response: " + resultBuffer.toString());
//        return resultBuffer.toString();
//    }
//
//    /**
//     * 上传文件
//     *
//     * @param url
//     * @param file
//     * @param name
//     * @return
//     * @throws Exception
//     */
//    public static String uploadFile(String url, File file, String name) throws Exception {
//        if (LOGGER.isDebugEnabled()) {
//            LOGGER.debug("==== 文件上传路径 {} 文件名 {}", url, file.getName());
//        }
//        String end = "\r\n";
//        String twoHyphens = "--";
//        String boundary = "---------------------------823928434";
//        name = null == name ? "file1" : name;
//
//        int bufferSize = 1024;
//        byte[] buffer = new byte[bufferSize];
//        int length;
//
//        DataOutputStream dataOutputStream = null;
//        FileInputStream fileInputStream = null;
//        InputStream inputStream = null;
//        InputStreamReader inputStreamReader = null;
//        BufferedReader reader = null;
//        StringBuffer resultBuffer = new StringBuffer();
//        String tempLine;
//        try {
//            URL localURL = new URL(url);
//            HttpURLConnection httpURLConnection = (HttpURLConnection) localURL.openConnection();
//            httpURLConnection.setDoInput(true);
//            httpURLConnection.setDoOutput(true);
//            httpURLConnection.setUseCaches(false);
//            httpURLConnection.setRequestMethod("POST");
//            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
//            httpURLConnection.setRequestProperty("Charset", "UTF-8");
//            httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
//
//            dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
//            dataOutputStream.writeBytes(twoHyphens + boundary + end);
//            dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + file.getName() + "\"" + end);
//            dataOutputStream.writeBytes(end);
//
//            fileInputStream = new FileInputStream(file);
//            while ((length = fileInputStream.read(buffer)) != -1) {
//                dataOutputStream.write(buffer, 0, length);
//            }
//
//            dataOutputStream.writeBytes(end);
//            dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + end);
//            dataOutputStream.flush();
//
//            // 读取服务器返回结果
//            inputStream = httpURLConnection.getInputStream();
//            inputStreamReader = new InputStreamReader(inputStream, CHARSET);
//            reader = new BufferedReader(inputStreamReader);
//            while ((tempLine = reader.readLine()) != null) {
//                resultBuffer.append(tempLine);
//            }
//
//        } finally {
//            if (dataOutputStream != null) {
//                dataOutputStream.close();
//            }
//
//            if (null != fileInputStream) {
//                fileInputStream.close();
//            }
//
//            if (reader != null) {
//                reader.close();
//            }
//
//            if (inputStreamReader != null) {
//                inputStreamReader.close();
//            }
//
//            if (inputStream != null) {
//                inputStream.close();
//            }
//        }
//
//        LOGGER.info("http uploadFile response: " + resultBuffer.toString());
//        return resultBuffer.toString();
//    }
//
//    /**
//     * json转key_value
//     *
//     * @param jsonStr
//     * @return
//     */
//    public static String json2KV(String jsonStr) throws Exception {
//        StringBuffer stringBuffer = new StringBuffer();
//        JSONObject jsonObject = JSON.parseObject(jsonStr);
//        jsonObject.forEach((s, o) -> stringBuffer.append(s).append("=").append(o).append("&"));
//        return StringUtils.left(stringBuffer.toString(), stringBuffer.length() - 1).replaceAll("\\+", "%2B");
//    }
//
//    /**
//     * key_value转json
//     *
//     * @param kvStr
//     * @return
//     * @throws Exception
//     */
//    public static String KV2Json(String kvStr) throws Exception {
//        String[] msgArray = kvStr.split("&");
//        JSONObject jsonObject = new JSONObject();
//        String[] keyValue = null;
//        for (int i = 0; i < msgArray.length; i++) {
//            keyValue = msgArray[i].split("=");
//            if (keyValue != null && keyValue.length == 2) {
//                jsonObject.put(keyValue[0], keyValue[1]);
//            }
//        }
//        return jsonObject.toJSONString();
//    }
//}
